import React from 'react';

function hoc(WithComponent) {
    return class hocComponent extends React.Component{
        constructor(){
            super();
            this.state={
                bAgent:false,
                bConent:false,
                bPrivacy:false,
                bStatement:false,
                bOImg:false,
                bWImg:false,
                bTImg:false,
            }

        }
        //点击跳转链接
        onClickGopage(url){
            let strUrl= "^(://)" +
                "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" +//ftp的user@+
            "(([0-9]{1,3}.){3}[0-9]{1,3}" +// IP形式的URL- 199.194.52.184 +
            "|" +// 允许IP和DOMAIN（域名） +
            "([0-9a-z_!~*'()-]+.)*" +// 域名- www. +
            "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]." +// 二级域名 +
            "[a-z]{0,6})" +// first level domain- .com or .museum +
            "(:[0-9]{1,4})?" +// 端口- :80 +
            "((/?)|"  +// a slash isn't required if there is no file name +
            "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
            let reg = new RegExp(strUrl);
            if (!reg.test(url)){
                window.open(url)
            }else {
                this.props.props.history.push(window.base.config.path+url)
            }
        }
        //点击显示代理商招募弹窗
        onClickShowPopup(){
            this.setState({bAgent:true})
        }
        //点击显示联系弹窗
        onClickContPopup(){
            this.setState({bConent:true})
        }
        //点击显示政策弹窗
        onClickPriPopup(){
            this.setState({bPrivacy:true})
        }
        //点击显示声明弹窗
        onClickStatPopup(){
            this.setState({bStatement:true})
        }
        //点击关闭弹窗
        onClickHiddenPopup(e){
            //阻止点击冒泡
            e.stopPropagation()
            this.setState({bAgent:false,bConent:false,bPrivacy:false,bStatement:false})
        }
        onMouseEnter(val){
            switch (val) {
                case 'bOImg':
                    this.setState({bOImg:true});
                    break;
                case 'bWImg':
                    this.setState({bWImg:true});
                    break
                case 'bTImg':
                    this.setState({bTImg:true});
                    break
                default:
                    return
            }
        }
        onMouseLeave(val){
            switch (val) {
                case 'bOImg':
                    this.setState({bOImg:false});
                    break;
                case 'bWImg':
                    this.setState({bWImg:false});
                    break
                case 'bTImg':
                    this.setState({bTImg:false});
                    break
                default:
                    return
            }
        }
        render(){
            let newProps={
                bAgent:this.state.bAgent,
                bConent:this.state.bConent,
                bPrivacy:this.state.bPrivacy,
                bStatement:this.state.bStatement,
                bOImg:this.state.bOImg,
                bWImg:this.state.bWImg,
                bTImg:this.state.bTImg,
                onClickGopage:this.onClickGopage,
                onClickShowPopup:this.onClickShowPopup,
                onClickContPopup:this.onClickContPopup,
                onClickPriPopup:this.onClickPriPopup,
                onClickStatPopup:this.onClickStatPopup,
                onClickHiddenPopup:this.onClickHiddenPopup,
                onMouseEnter:this.onMouseEnter,
                onMouseLeave:this.onMouseLeave
            }

            return(
                <WithComponent {...this.props} {...newProps}></WithComponent>
            )
        }
    }
}
export default hoc